<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <input type="text" id="i1">
    <input type="button" value="平方" onclick="f()">
    <div>结果:<span>0</span></div>
    <script>
        function f() {
            //获取输入框
            let i = document.querySelector("#i1");
            //获取span
            let s = document.querySelector("span");
            //"5"*"5" 5*5
            //"a"*"a" NaN*NaN = NaN
            //isNaN()判断变量是否是NaN
            if (isNaN(i.value)) {
                s.innerText = "输入错误!";
                return;
            }
            /*s.innerText = i.value * i.value;*/
            //假如做其他运算,当两个字符串进行-*/运算时会自动转换为数值,但是进行+运算则会变为拼接
            //s.innerText = i.value*1 + i.value*1; 第一种方式,强制转换为数值
            //第二种方式,parseFloat() 将字符串转换为小数或整数
            s.innerText = parseFloat(i.value) + parseFloat(i.value);
        }
    </script>
</body>
</html>